
#include "stdafx.h"
#include "ConstDef.h"
#include "Logger.h"
#include "Interface.h"
#include "IoBuffer.h"
#include "IPCCallPacket.h"

namespace IPCCall
{
	SerializeString::SerializeString()
	{

	}

	SerializeString::SerializeString( const std::vector<byte>& _array )
	{
		R0(setValue(_array));
	}

	SerializeString::SerializeString( const char* _cstr )
	{
		R0(setValue(_cstr));
	}

	SerializeString::SerializeString( const std::string& _str )
	{
		R0(setValue(_str));
	}

	SerializeString::~SerializeString()
	{

	}

	retT SerializeString::setValue( const std::vector<byte>& _array )
	{
		array_.clear();
		array_.resize(_array.size());
		std::copy(_array.begin(), _array.end(), array_.begin());
		RETSUCCESS;
	}

	retT SerializeString::setValue( const char* _cstr )
	{
		array_.clear();
		array_.resize(strlen(_cstr));
		for(std::size_t i = 0; i < array_.size(); ++i)
			array_[i] = (byte)_cstr[i];
		RETSUCCESS;
	}

	retT SerializeString::setValue( const std::string& _str )
	{
		array_.clear();
		array_.resize(_str.length());
		for(std::size_t i = 0; i < array_.size(); ++i)
			array_[i] = (byte)_str[i];
		RETSUCCESS;
	}

	std::string SerializeString::toString()
	{
		if(array_.size() > 0)
			return std::string((char*)&array_[0], array_.size());
		return std::string();
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	IPCCallPacketBase::IPCCallPacketBase()
	{

	}

	IPCCallPacketBase::~IPCCallPacketBase()
	{

	}

	retT IPCCallPacketBase::readFromBufferNoCheck( boost::shared_ptr<IoBuffer> _pBuffer )
	{
		R(messageQueueName_.readFromBuffer(_pBuffer));
		R(overlayInstanceName_.readFromBuffer(_pBuffer));
		R(resourceName_.readFromBuffer(_pBuffer));
		RETSUCCESS;
	}

	retT IPCCallPacketBase::writeToBufferNoCheck( boost::shared_ptr<IoBuffer> _pBuffer )
	{
		R(messageQueueName_.writeToBuffer(_pBuffer));
		R(overlayInstanceName_.writeToBuffer(_pBuffer));
		R(resourceName_.writeToBuffer(_pBuffer));
		RETSUCCESS;
	}

	retT IPCCallPacketBase::readFromBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(readFromBufferNoCheck(_pBuffer));
		READFROMBUFFER_END;
	}

	retT IPCCallPacketBase::writeToBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(writeToBufferNoCheck(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t IPCCallPacketBase::size()
	{
		return messageQueueName_.size() + overlayInstanceName_.size() + resourceName_.size();
	}

	retT IPCCallPacketBase::attachRootElement( boost::weak_ptr<PacketElement> _pRootElement )
	{
		R(PacketElement::attachRootElement(_pRootElement));
		R(messageQueueName_.attachRootElement(_pRootElement));
		R(overlayInstanceName_.attachRootElement(_pRootElement));
		R(resourceName_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	SendStoreReqIPCCallPacket::SendStoreReqIPCCallPacket()
	{

	}

	SendStoreReqIPCCallPacket::~SendStoreReqIPCCallPacket()
	{

	}

	retT SendStoreReqIPCCallPacket::readFromBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(readFromBufferNoCheck(_pBuffer));
		R(kindData_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT SendStoreReqIPCCallPacket::writeToBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(writeToBufferNoCheck(_pBuffer));
		R(kindData_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t SendStoreReqIPCCallPacket::size()
	{
		return IPCCallPacketBase::size() + kindData_.size();
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	SendFetchReqIPCCallPacket::SendFetchReqIPCCallPacket()
	{

	}

	SendFetchReqIPCCallPacket::~SendFetchReqIPCCallPacket()
	{

	}

	retT SendFetchReqIPCCallPacket::readFromBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(readFromBufferNoCheck(_pBuffer));
		R(specifiers_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT SendFetchReqIPCCallPacket::writeToBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(writeToBufferNoCheck(_pBuffer));
		R(specifiers_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t SendFetchReqIPCCallPacket::size()
	{
		return IPCCallPacketBase::size() + specifiers_.size();
	}

	retT SendFetchReqIPCCallPacket::attachRootElement( boost::weak_ptr<PacketElement> _pRootElement )
	{
		R(IPCCallPacketBase::attachRootElement(_pRootElement));
		R(specifiers_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	IPCCallResultBase::IPCCallResultBase()
		:result_(0)
	{

	}

	IPCCallResultBase::~IPCCallResultBase()
	{
		result_ = 0;
	}

	retT IPCCallResultBase::readFromBufferNoCheck( boost::shared_ptr<IoBuffer> _pBuffer )
	{
		R(_pBuffer->ntohRead(result_));
		RETSUCCESS;
	}

	retT IPCCallResultBase::writeToBufferNoCheck( boost::shared_ptr<IoBuffer> _pBuffer )
	{
		R(_pBuffer->htonWrite(result_));
		RETSUCCESS;
	}

	retT IPCCallResultBase::readFromBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(readFromBufferNoCheck(_pBuffer));
		READFROMBUFFER_END;
	}

	retT IPCCallResultBase::writeToBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(writeToBufferNoCheck(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t IPCCallResultBase::size()
	{
		return sizeof(result_);
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	SendFetchReqIPCCallResult::SendFetchReqIPCCallResult()
	{

	}

	SendFetchReqIPCCallResult::~SendFetchReqIPCCallResult()
	{

	}

	retT SendFetchReqIPCCallResult::readFromBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(readFromBufferNoCheck(_pBuffer));
		R(kindResponses_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT SendFetchReqIPCCallResult::writeToBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(writeToBufferNoCheck(_pBuffer));
		R(kindResponses_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t SendFetchReqIPCCallResult::size()
	{
		return IPCCallResultBase::size() + kindResponses_.size();
	}

	retT SendFetchReqIPCCallResult::attachRootElement( boost::weak_ptr<PacketElement> _pRootElement )
	{
		R(PacketElement::attachRootElement(_pRootElement));
		R(kindResponses_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	SendAppAttachReqIPCCallResult::SendAppAttachReqIPCCallResult()
	{

	}

	SendAppAttachReqIPCCallResult::~SendAppAttachReqIPCCallResult()
	{

	}

	retT SendAppAttachReqIPCCallResult::readFromBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(readFromBufferNoCheck(_pBuffer));
		R(strIP_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT SendAppAttachReqIPCCallResult::writeToBuffer( boost::shared_ptr<IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(writeToBufferNoCheck(_pBuffer));
		R(strIP_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t SendAppAttachReqIPCCallResult::size()
	{
		return IPCCallResultBase::size() + strIP_.size();
	}

	retT SendAppAttachReqIPCCallResult::attachRootElement( boost::weak_ptr<PacketElement> _pRootElement )
	{
		R(PacketElement::attachRootElement(_pRootElement));
		R(strIP_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}
}
